import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/assessments.dart';
import 'package:ichinesereader/models/assessment/assessmentBookType.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/buttons/floatingActionButtonCreateAssessment.dart';
import 'package:ichinesereader/widgets/grids/assessmentBookGridWithCheckBox.dart';
import '../../themes/appTheme.dart';

class AssessmentBooksTeacherPage extends StatefulWidget {
  @override
  State<AssessmentBooksTeacherPage> createState() =>
      _AssessmentBooksTeacherPageState();
}

class _AssessmentBooksTeacherPageState
    extends State<AssessmentBooksTeacherPage> {
  final AssessmentsController _controller = Get.find();
  AssessmentBookType type = Constants.assessmentBookTypes[0];

  @override
  void initState() {
    _controller.assessmentBenchmarkTests.clear();

    Future.delayed(Duration(milliseconds: 100), () {
      _controller.setCurrentAssessmenbBookType(type);
      _controller.fetchAssessmentBenchmarkBooks(
          '0', '0', type.assessmentType, '');
    });
    super.initState();
  }

  @override
  void dispose() {
    _controller.assessmentBenchmarkTests.clear();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build Assessment Books TeacherPage');
    return Scaffold(
        backgroundColor: Colors.transparent,
        body: Column(
          children: [
            Container(
              margin: EdgeInsets.only(top: 3.0),
              height: 39.0,
              child: FormBuilder(
                child: FormBuilderChoiceChip(
                  alignment: WrapAlignment.end,
                  spacing: 3.0,
                  labelStyle: TextStyle(
                    color: Get.isDarkMode
                        ? (Colors.black)
                        : AppTheme.colors.lightOrange,
                    fontSize: 15,
                    letterSpacing: 1,
                  ),
                  decoration: InputDecoration.collapsed(
                    hintText: '',
                    border: InputBorder.none,
                  ),
                  backgroundColor:
                      Get.isDarkMode ? Get.theme.primaryColor : Colors.grey,
                  //initialValue: _controller.currentAssessmentBookType.value,
                  selectedColor:
                      Get.isDarkMode ? Colors.white : AppTheme.colors.orangeBtn,
                  //initialValue: _controller.getCurrentAssessmentBookType(),
                  initialValue: type,
                  name: 'assessment_type_chip',
                  onChanged: (value) {
                    if (value == null) return;

                    _controller.setCurrentAssessmenbBookType(
                        value as AssessmentBookType);
                    type = value;
                    _controller.selectedAssessmentBenchmarkTests.clear();
                    _controller.assessmentBenchmarkTests.clear();
                    _controller.fetchAssessmentBenchmarkBooks(
                        '0', '0', value.assessmentType, '');
                  },
                  options: Constants.assessmentBookTypes
                      .map((type) => FormBuilderChipOption<AssessmentBookType>(
                            value: type,
                            child: Text(
                              type.displayName.toString(),
                              overflow: TextOverflow.fade,
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                  fontSize: 12.0,
                                  color: Get.isDarkMode
                                      ? Colors.black
                                      : AppTheme.colors.lightOrange),
                            ),
                          ))
                      .toList(),
                ),
              ),
            ),
            Expanded(
                child: _controller.obx(
              (data) => AssessmentBookGridWithCheckBox(
                books: _controller.assessmentBenchmarkTests,
                readingType: NanhaiBookReadingTypes.assessment,
              ),
              onEmpty: Center(child: Text('Coming soon ...'.tr)),
              onLoading: Container(),
              // //onEmpty: Center(child: CircularProgressIndicator()),
              // onLoading: Center(
              //   child: CircularProgressIndicator(
              //     color: Get.isDarkMode
              //         ? Colors.white
              //         : Get.theme.primaryColorDark,
              //     backgroundColor: Get.isDarkMode
              //         ? Get.theme.primaryColorDark.withOpacity(0.3)
              //         : Get.theme.primaryColorDark.withOpacity(0.3),
              //     strokeWidth: 9.0,
              //   ),
              // ),
            )),
          ],
        ),
        floatingActionButton: FloatingActionButtonCreateAssessment());
  }
}
